﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace LMSemir.Common
{
    public class TEEUtil
    {
        /// <summary>
        /// 数据分页构造
        /// </summary>
        /// <param name="pIndex">当前页</param>
        /// <param name="pSize">页大小</param>
        /// <param name="pages">总页数</param>
        /// <param name="filePath">导航页面</param>
        /// <returns></returns>
        public static string GeneratePager(int pIndex, int pSize, int pages, int totalcount, string filePath, string urlParam)
        {
            string param = filePath.IndexOf('?') > -1 ? "&page=" : "?page=";

            int minPages = 10 > pages ? pages : 10;
            int maxPages = pIndex + 10 > pages ? pages : pIndex + 1;

            StringBuilder strPager = new StringBuilder();
            if (pIndex == pages)
            {
                if (pSize < totalcount)
                {
                    strPager.Append("<a href=\"" + filePath + param + Convert.ToInt16(pIndex - 1) + "" + urlParam + "\">上一页</a>");
                }
                else
                {
                    strPager.Append("<span class=\"disabled\">上一页</span>");
                }
                if (pIndex > minPages && pIndex < pages)
                {
                    for (int i = pIndex; i < maxPages + 1; i++)
                    {
                        if (i == pIndex)
                        {
                            strPager.Append("<span class=\"current\">" + pIndex + "</span>");
                        }
                        else
                        {
                            strPager.Append("<a href=\"" + filePath + param + i + "" + urlParam + "\">" + i + "</a>");
                        }
                    }
                }
                else if (pIndex < pages)
                {
                    for (int i = 1; i < minPages + 1; i++)
                    {
                        if (i != pIndex)
                        {
                            strPager.Append("<a href=\"" + filePath + param + i + "" + urlParam + "\">" + i + "</a>");
                        }
                        else
                        {
                            strPager.Append("<span class=\"current\">" + pIndex + "</span>");
                        }
                    }
                }
                strPager.Append("<span class=\"disabled\">下一页</span>");
            }
            else if (pIndex > 1 && pIndex < pages)
            {
                strPager.Append("<a href=\"" + filePath + param + Convert.ToInt16(pIndex - 1) + "" + urlParam + "\">上一页</a>");
                if (pIndex > minPages && pIndex < pages)
                {


                    for (int i = pIndex; i < maxPages + 1; i++)
                    {
                        if (i == pIndex)
                        {
                            strPager.Append("<span class=\"current\">" + pIndex + "</span>");
                        }
                        else
                        {
                            strPager.Append("<a href=\"" + filePath + param + i + "" + urlParam + "\">" + i + "</a>");
                        }
                    }
                }
                else
                {
                    for (int i = 1; i < minPages + 1; i++)
                    {
                        if (i == pIndex)
                        {
                            strPager.Append("<span class=\"current\">" + pIndex + "</span>");
                        }
                        else
                        {
                            strPager.Append("<a href=\"" + filePath + param + i + "" + urlParam + "\">" + i + "</a>");
                        }
                    }
                }
                strPager.Append("...");
                strPager.Append("<a href=\"" + filePath + param + Convert.ToInt16(pIndex + 1) + "" + urlParam + "\">下一页</a>");
            }
            else if (pIndex == 1 && pIndex < pages)
            {
                strPager.Append("<span class=\"disabled\">上一页</span>");
                for (int i = 1; i < minPages + 1; i++)
                {
                    if (i == pIndex)
                    {
                        strPager.Append("<span class=\"current\">" + pIndex + "</span>");
                    }
                    else
                    {
                        strPager.Append("<a href=\"" + filePath + param + i + "" + urlParam + "\">" + i + "</a>");
                    }
                }
                strPager.Append("...");
                strPager.Append("<a href=\"" + filePath + param + Convert.ToInt16(pIndex + 1) + "" + urlParam + "\">下一页</a>");
            }
            return strPager.ToString();
        }

        public static string WriteSourceName(int sourceID)
        {
            switch (sourceID)
            {
                case 1:
                    return "<span style='color:red;'>森马</span>";
                case 2:
                    return "<span style='color:#FE6C00;'>土豆网</span>";
                case 3:
                    return "<span style='color:#3FA156;'>豆瓣网</span>";
                default:
                    return "<span style='color:red;'>全部</span>";
            }
        }
        public static string AnaylzsisStatus(int status)
        {
            switch (status)
            {
                case 0:
                    return "正常";
                case -1:
                    return "已删除";
                case 1:
                    return "已入围";
                case 2:
                    return "最潮的";
                case 3:
                    return "已获奖";
                case 4:
                    return "审核成功<img src=\"images/form_success.png\" style=\"vertical-align:middle\"/>";
                default:
                    return "待处理";
            }
        }
        public static string AnaylzsisCategory(int cid)
        {
            switch (cid)
            {
                case 1:
                    return "最热类";
                case 2:
                    return "视频类";
                case 3:
                    return "原创类";
                case 4:
                    return "艺术家类";
                case 5:
                    return "名人类";
                case 6:
                    return "最新类";
                default:
                    return "默认分类";
            }
        }

        //生成XML数据
        public static string generateTeeXML(DataTable dt, int rowCount)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendFormat("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            strBuilder.AppendFormat("<results>");
            if (dt.Rows.Count > 0)
            {
                strBuilder.AppendFormat("<flag><![CDATA[1]]></flag>\r");
                strBuilder.AppendFormat("<counts><![CDATA[{0}]]></counts>\r", rowCount);
                strBuilder.AppendFormat("<tees>\r");

                foreach (DataRow dr in dt.Rows)
                {
                    strBuilder.AppendFormat("<tee id=\"{0}\">", dr["tid"].ToString());
                    strBuilder.AppendFormat("<Name><![CDATA[{0}]]></Name>", dr["TName"].ToString());
                    strBuilder.AppendFormat("<Author><![CDATA[{0}]]></Author>", dr["TAuthor"].ToString());
                    strBuilder.AppendFormat("<Email><![CDATA[{0}]]></Email>\r", dr["TEmail"].ToString());
                    strBuilder.AppendFormat("<Mobile><![CDATA[{0}]]></Mobile>\r", dr["TMobile"].ToString());
                    strBuilder.AppendFormat("<Address><![CDATA[{0}]]></Address>\r", dr["TAddress"].ToString());
                    strBuilder.AppendFormat("<Source><![CDATA[{0}]]></Source>\r", dr["TSource"].ToString());
                    strBuilder.AppendFormat("<Intro><![CDATA[{0}]]></Intro>\r", dr["TIntro"].ToString());
                    strBuilder.AppendFormat("<Video><![CDATA[{0}]]></Video>\r", dr["TVideo"].ToString());
                    strBuilder.AppendFormat("<PhotoPath><![CDATA[{0}]]></PhotoPath>\r", dr["TPhotoPath"].ToString());
                    strBuilder.AppendFormat("<PhotoName><![CDATA[{0}]]></PhotoName>\r", dr["TPhotoName"].ToString());
                    strBuilder.AppendFormat("<SmallImage><![CDATA[{0}]]></SmallImage>\r", dr["TSmallImage"].ToString());
                    strBuilder.AppendFormat("<MiddImage><![CDATA[{0}]]></MiddImage>", dr["TMiddImage"].ToString());
                    strBuilder.AppendFormat("<LargeImage><![CDATA[{0}]]></LargeImage>\r", dr["TLargeImage"].ToString());
                    strBuilder.AppendFormat("<CreateTS><![CDATA[{0}]]></CreateTS>\r", Convert.ToDateTime(dr["TCreateTS"]));
                    strBuilder.AppendFormat("<UpdateTS><![CDATA[{0}]]></UpdateTS>\r", Convert.ToDateTime(dr["TUpdateTS"]));
                    strBuilder.AppendFormat("<TJoin><![CDATA[{0}]]></TJoin>\r", Convert.ToInt32(dr["TJoin"]));
                    strBuilder.AppendFormat("<TFashion><![CDATA[{0}]]></TFashion>\r", Convert.ToInt32(dr["TFashion"]));
                    strBuilder.AppendFormat("<TWin><![CDATA[{0}]]></TWin>\r", Convert.ToInt32(dr["TWin"]));

                    strBuilder.AppendFormat("<UID><![CDATA[{0}]]></UID>\r", Convert.ToInt32(dr["TUID"]));
                    strBuilder.AppendFormat("<Status><![CDATA[{0}]]></Status>\r", Convert.ToInt32(dr["TStatus"]));
                    strBuilder.AppendFormat("<VoteCount><![CDATA[{0}]]></VoteCount>\r", Convert.IsDBNull(dr["TAVoteCount"]) == true ? 0 : Convert.ToInt32(dr["TAVoteCount"]));
                    strBuilder.AppendFormat("<voteCreateTS><![CDATA[{0}]]></voteCreateTS>", Convert.IsDBNull(dr["voteCreateTS"]) == true ? DateTime.Now : Convert.ToDateTime(dr["voteCreateTS"]));
                    strBuilder.AppendFormat("<voteUpdateTS><![CDATA[{0}]]></voteUpdateTS>", Convert.IsDBNull(dr["voteCreateTS"]) == true ? DateTime.Now : Convert.ToDateTime(dr["voteUpdateTS"]));
                    strBuilder.AppendFormat("</tee>\r");
                }
                strBuilder.AppendFormat("</tees>\r");
            }
            else
            {
                strBuilder.AppendFormat("<flag><![CDATA[-1]]></flag>\r");
                strBuilder.AppendFormat("<counts><![CDATA[{0}]]></counts>\r", rowCount);
            }
            strBuilder.AppendFormat("</results>");
            return strBuilder.ToString();
        }
        public static string generateTeeXML(DataTable dt)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendFormat("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            strBuilder.AppendFormat("<results>");
            if (dt.Rows.Count > 0)
            {
                strBuilder.AppendFormat("<flag><![CDATA[1]]></flag>\r");
                strBuilder.AppendFormat("<counts><![CDATA[{0}]]></counts>\r", dt.Rows.Count);
                strBuilder.AppendFormat("<tees>\r");

                foreach (DataRow dr in dt.Rows)
                {
                    strBuilder.AppendFormat("<tee id=\"{0}\">", dr["tid"].ToString());
                    strBuilder.AppendFormat("<Name><![CDATA[{0}]]></Name>", dr["TName"].ToString());
                    strBuilder.AppendFormat("<Author><![CDATA[{0}]]></Author>", dr["TAuthor"].ToString());
                    strBuilder.AppendFormat("<Email><![CDATA[{0}]]></Email>\r", dr["TEmail"].ToString());
                    strBuilder.AppendFormat("<Mobile><![CDATA[{0}]]></Mobile>\r", dr["TMobile"].ToString());
                    strBuilder.AppendFormat("<Address><![CDATA[{0}]]></Address>\r", dr["TAddress"].ToString());
                    strBuilder.AppendFormat("<Source><![CDATA[{0}]]></Source>\r", dr["TSource"].ToString());
                    strBuilder.AppendFormat("<Intro><![CDATA[{0}]]></Intro>\r", dr["TIntro"].ToString());
                    strBuilder.AppendFormat("<Video><![CDATA[{0}]]></Video>\r", dr["TVideo"].ToString());
                    strBuilder.AppendFormat("<PhotoPath><![CDATA[{0}]]></PhotoPath>\r", dr["TPhotoPath"].ToString());
                    strBuilder.AppendFormat("<PhotoName><![CDATA[{0}]]></PhotoName>\r", dr["TPhotoName"].ToString());
                    strBuilder.AppendFormat("<SmallImage><![CDATA[{0}]]></SmallImage>\r", dr["TSmallImage"].ToString());
                    strBuilder.AppendFormat("<MiddImage><![CDATA[{0}]]></MiddImage>", dr["TMiddImage"].ToString());
                    strBuilder.AppendFormat("<LargeImage><![CDATA[{0}]]></LargeImage>\r", dr["TLargeImage"].ToString());
                    strBuilder.AppendFormat("<CreateTS><![CDATA[{0}]]></CreateTS>\r", Convert.ToDateTime(dr["TCreateTS"]));
                    strBuilder.AppendFormat("<UpdateTS><![CDATA[{0}]]></UpdateTS>\r", Convert.ToDateTime(dr["TUpdateTS"]));
                    strBuilder.AppendFormat("<TJoin><![CDATA[{0}]]></TJoin>\r", Convert.ToInt32(dr["TJoin"]));
                    strBuilder.AppendFormat("<TFashion><![CDATA[{0}]]></TFashion>\r", Convert.ToInt32(dr["TFashion"]));
                    strBuilder.AppendFormat("<TWin><![CDATA[{0}]]></TWin>\r", Convert.ToInt32(dr["TWin"]));

                    strBuilder.AppendFormat("<UID><![CDATA[{0}]]></UID>\r", Convert.ToInt32(dr["TUID"]));
                    strBuilder.AppendFormat("<Status><![CDATA[{0}]]></Status>\r", Convert.ToInt32(dr["TStatus"]));
                    strBuilder.AppendFormat("<VoteCount><![CDATA[{0}]]></VoteCount>\r", Convert.IsDBNull(dr["TAVoteCount"]) == true ? 0 : Convert.ToInt32(dr["TAVoteCount"]));
                    strBuilder.AppendFormat("<voteCreateTS><![CDATA[{0}]]></voteCreateTS>", Convert.IsDBNull(dr["voteCreateTS"]) == true ? DateTime.Now : Convert.ToDateTime(dr["voteCreateTS"]));
                    strBuilder.AppendFormat("<voteUpdateTS><![CDATA[{0}]]></voteUpdateTS>", Convert.IsDBNull(dr["voteCreateTS"]) == true ? DateTime.Now : Convert.ToDateTime(dr["voteUpdateTS"]));
                    strBuilder.AppendFormat("</tee>\r");
                }
                strBuilder.AppendFormat("</tees>\r");
            }
            else
            {
                strBuilder.AppendFormat("<flag><![CDATA[-1]]></flag>\r");
                strBuilder.AppendFormat("<counts><![CDATA[{0}]]></counts>\r", dt.Rows.Count);
            }
            strBuilder.AppendFormat("</results>");
            return strBuilder.ToString();
        }
        //generate xml format tee data by opreate code
        public static string generateTeeXML(int opCode)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendFormat("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r");
            strBuilder.AppendFormat("<results>\r");
            strBuilder.AppendFormat("<flag><![CDATA[{0}]]></flag>\r", opCode.ToString());
            strBuilder.AppendFormat("<counts><![CDATA[{0}]]></counts>\r", opCode.ToString());
            strBuilder.AppendFormat("</results>");
            return strBuilder.ToString();
        }
        public static string generateTeeXML(int opCode, string elementKey, string elementValue)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendFormat("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r");
            strBuilder.AppendFormat("<results>\r");
            strBuilder.AppendFormat("<flag><![CDATA[{0}]]></flag>\r", opCode.ToString());
            strBuilder.AppendFormat("<counts><![CDATA[0]]></counts>\r");
            if (!string.IsNullOrEmpty(elementKey) && !string.IsNullOrEmpty(elementValue))
            {
                strBuilder.AppendFormat("<{0}><![CDATA[{1}]]></{0}>\r", elementKey, elementValue);
            }
            strBuilder.AppendFormat("</results>");
            return strBuilder.ToString();
        }

        /// <summary>
        /// 解析TEE来源
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int AnaylsisTeeSource(string source)
        {
            switch (source)
            {
                case "tudou":
                    return 2;
                case "douban":
                    return 3;
                case "semir":
                    return 1;
                default:
                    return 1;
            }
        }
        /// <summary>
        /// 反序列化JSON数据为DataTable格式
        /// </summary>
        /// <param name="jsons"></param>
        /// <returns></returns>
        public static DataTable GetTeeResults(string jsons)
        {
            DataTable dtTee = new DataTable();
            dtTee = (DataTable)JsonConvert.DeserializeObject(jsons, typeof(DataTable));
            return dtTee;
        }
        //anaylzesis teeinfo source
        public static string AnaylzeSource(int sourceID)
        {
            switch (sourceID)
            {
                case 1:
                    return "森马";
                case 2:
                    return "土豆网";
                case 3:
                    return "豆瓣网";
                default:
                    return "未知";
            }
        }
        //anaylzsis user's email
        public static string AnaylzsisEmail(string email)
        {
            return email.Length != 0 ? email : "暂无";
        }
    }
}
